package com.ycy.temperature.view.fragment;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.jude.utils.JUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ycy.temperature.R;
import com.ycy.temperature.adapter.CircleAdapter;
import com.ycy.temperature.bean.DynamicBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.view.base.BaseFragment;
import com.ycy.temperature.view.view.TitleNewView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;

/**
 * Created by MI on 2018/7/17.
 */

public class CircleFragment extends BaseFragment {
    @BindView(R.id.title)
    TitleNewView title;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    Unbinder unbinder;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private CircleAdapter mAdapter;
    private List<DynamicBean.DataBean> mData = new ArrayList<>();
    int i = 0;

    @Override
    protected int getRes() {
        return R.layout.fragment_circle;
    }

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        initView();
    }

    @Override
    protected void setData(View view) {

    }

    @Override
    public void onClick(View v) {

    }

    private void initView() {
        mAdapter = new CircleAdapter(mData, getContext());
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
//        smartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
//            @Override
//            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
//                //开始设置全局的基本参数（这里设置的属性只跟下面的MaterialHeader绑定，其他Header不会生效，能覆盖DefaultRefreshInitializer的属性和Xml设置的属性）
//                layout.setEnableHeaderTranslationContent(false);
//                return new MaterialHeader(context).setColorSchemeResources(R.color.bg_Blue, R.color.bg_Blue, R.color.bg_Blue);
//            }
//        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        i = 0;
                        setDynamic("0");
                        refreshLayout.finishRefresh();
                        if (mAdapter != null) {
                            mData.clear();
                            mAdapter.clearList();
                        }
                    }
                }, 2000);
            }
        });

        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setDynamic(i + "");
                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });
        smartRefreshLayout.setEnableLoadMore(true);
//        // 进入页面后自动刷新
        smartRefreshLayout.autoRefresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void setDynamic(final String str) {
        Log.e("strasdasd", str);
        UserNetWorks.setDynamic("1", str, "10", new Subscriber<DynamicBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(DynamicBean dynamicBean) {
                if (dynamicBean.getResponseState().equals("1")) {
                    Log.e("dynamicBean", mAdapter.getItemCount() + "");
                    mData.addAll(dynamicBean.getData());
                    if (dynamicBean.getData() != null && dynamicBean.getData().size() > 0) {
                        i++;
                    }
                }
            }
        });
    }
}
